package dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import dao.AdminDao;
import dao.NewsDao;
import dao.RowMapper;
import entity.News;
//HanMan
public class NewsDaoImpl implements NewsDao {
	JDBCTemplate jdbc = new JDBCTemplate();
	AdminDao ad = new AdminDaoImpl();
	private class rowMapperImpl implements RowMapper<News>{
		@Override
		public News rowMapper(ResultSet rs) throws SQLException {
			//newsId, title, content, publishDate, admin
			return new News(rs.getInt(1), rs.getString(2),rs.getString(3),rs.getDate(4),ad.queryAdminByIdAdminId(rs.getInt(5)));
		}
	}
	@Override
	public List<News> queryAllNews() { 
		return jdbc.queryAll("SELECT * FROM news ORDER BY publishDate DESC", new rowMapperImpl());
	}

	@Override
	public List<News> queryAllNews(int pageNum, int pageSize) {
		return jdbc.queryByPaging(queryAllNews(), pageNum, pageSize);
	}

	@Override
	public News queryNewByNewsId(int newsId) {
		return jdbc.queryOne("select * from news where newsId=?", new rowMapperImpl(),newsId);
	}

}
